package section3;

import java.util.Scanner;
public class PracticeIII {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int n,m;
		String[] nums,tm,ta;
		n = sc.nextInt();
		while((n--)>0){
			m = sc.nextInt();
			String numM = "",numA = "",startTime = "",endTime = "";
			nums = new String[m];
			tm = new String[m];
			ta = new String[m];
			for(int i=0; i<m; i++){
				nums[i] = sc.next();
				tm[i] = sc.next();
				ta[i] = sc.next();
				if(i==0){
					startTime = tm[i];
					endTime = ta[i];
				}
				if(startTime.compareTo(tm[i])>=0){
					startTime = tm[i];
					numM = nums[i];
				}
				if(endTime.compareTo(ta[i])<=0){
					endTime = ta[i];
					numA = nums[i];
				}
			}
			System.out.println(numM+" "+numA);
		}
	}
}

/*
开门人和关门人
Sample Input
3
1
ME3021112225321 00:00:00 23:59:59
2
EE301218 08:05:35 20:56:35
MA301134 12:35:45 21:40:42
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output
ME3021112225321 ME3021112225321
EE301218 MA301134
SC3021234 CS301133

import java.io.BufferedInputStream;  
import java.util.Date;  
import java.util.Scanner;  
  
import com.sun.org.apache.regexp.internal.recompile;  
  
public class Main {  
  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));  
        int days = sc.nextInt();// days of the records  
        for (int i = 0; i < days; i++) {  
            int details = sc.nextInt();  
            String id[] = new String[details];  
            String start[] = new String[details];  
            String end[] = new String[details];  
            String earliest = "23:59:59";  
            String lastest = "00:00:00";  
            String earliestMan = null;  
            String lastestMan = null;  
  
            for (int j = 0; j < details; j++) {  
                id[j] = sc.next();  
                start[j] = sc.next();  
                end[j] = sc.next();  
                if (start[j].compareTo(earliest) <= 0) {  
                    // if the start-time of the worker are earlier than the recoreded-easiest-time  
                    earliest = start[j];  
                    earliestMan = id[j];  
                }  
                if (lastest.compareTo(end[j]) <= 0) {  
                    // if the end-time of the worker are later  
                    lastest = end[j];  
                    lastestMan = id[j];  
                }  
            }  
            System.out.println(earliestMan + " " + lastestMan);  
        }  
    }  
}  

额外方法说明：
compareTo方法：对象1.compareTo(对象2)；



 * */